package cn.danger.com.fragment.schedule;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;

/**
 * Created by 俊 on 2017/1/3.
 */
public class RemindRingFragment extends BackHandledFragment implements View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private RelativeLayout ring1_layout;
    private ImageView ring1_imageView;
    private RelativeLayout ring2_layout;
    private ImageView ring2_imageView;
    private RelativeLayout ring3_layout;
    private ImageView ring3_imageView;
    private RelativeLayout ring4_layout;
    private ImageView ring4_imageView;
    private RelativeLayout ring5_layout;
    private ImageView ring5_imageView;
    private RelativeLayout ring6_layout;
    private ImageView ring6_imageView;
    private Handler handler;
    private String ring = "苹果line";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_remind_ring_layout, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);

        ring1_layout = (RelativeLayout) view.findViewById(R.id.ring1_layout);
        ring1_imageView = (ImageView) view.findViewById(R.id.ring1_imageView);
        ring2_layout = (RelativeLayout) view.findViewById(R.id.ring2_layout);
        ring2_imageView = (ImageView) view.findViewById(R.id.ring2_imageView);
        ring3_layout = (RelativeLayout) view.findViewById(R.id.ring3_layout);
        ring3_imageView = (ImageView) view.findViewById(R.id.ring3_imageView);
        ring4_layout = (RelativeLayout) view.findViewById(R.id.ring4_layout);
        ring4_imageView = (ImageView) view.findViewById(R.id.ring4_imageView);
        ring5_layout = (RelativeLayout) view.findViewById(R.id.ring5_layout);
        ring5_imageView = (ImageView) view.findViewById(R.id.ring5_imageView);
        ring6_layout = (RelativeLayout) view.findViewById(R.id.ring6_layout);
        ring6_imageView = (ImageView) view.findViewById(R.id.ring6_imageView);

        Bundle bundle = getArguments();

        if (bundle != null) {
            ring = bundle.getString("index");
            reset();
            if (ring.equals("苹果line")) {
                ring1_imageView.setVisibility(View.VISIBLE);
            } else if (ring.equals("口哨")) {
                ring2_imageView.setVisibility(View.VISIBLE);
            } else if (ring.equals("闹钟")) {
                ring3_imageView.setVisibility(View.VISIBLE);
            } else if (ring.equals("山谷回响")) {
                ring4_imageView.setVisibility(View.VISIBLE);
            } else if (ring.equals("低音")) {
                ring5_imageView.setVisibility(View.VISIBLE);
            } else if (ring.equals("清新")) {
                ring6_imageView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        ring1_layout.setOnClickListener(this);
        ring2_layout.setOnClickListener(this);
        ring3_layout.setOnClickListener(this);
        ring4_layout.setOnClickListener(this);
        ring5_layout.setOnClickListener(this);
        ring6_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        reset();
        switch (v.getId()) {
            case R.id.ring1_layout:
                ring1_imageView.setVisibility(View.VISIBLE);
                ring = "苹果line";
                break;
            case R.id.ring2_layout:
                ring2_imageView.setVisibility(View.VISIBLE);
                ring = "口哨";
                break;
            case R.id.ring3_layout:
                ring3_imageView.setVisibility(View.VISIBLE);
                ring = "闹钟";
                break;
            case R.id.ring4_layout:
                ring4_imageView.setVisibility(View.VISIBLE);
                ring = "山谷回响";
                break;
            case R.id.ring5_layout:
                ring5_imageView.setVisibility(View.VISIBLE);
                ring = "低音";
                break;
            case R.id.ring6_layout:
                ring6_imageView.setVisibility(View.VISIBLE);
                ring = "清新";
                break;
            default:
                break;
        }
        back();
    }

    private void back() {
        if (handler != null) {
            Message message = Message.obtain();
            message.what = 2;
            message.obj = ring;
            handler.sendMessage(message);
        }
        getFragmentManager().popBackStack();
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    private void reset() {
        ring1_imageView.setVisibility(View.GONE);
        ring2_imageView.setVisibility(View.GONE);
        ring3_imageView.setVisibility(View.GONE);
        ring4_imageView.setVisibility(View.GONE);
        ring5_imageView.setVisibility(View.GONE);
        ring6_imageView.setVisibility(View.GONE);
    }
}
